package cn.bestick.weightrecord.service;

import cn.bestick.weightrecord.dao.RecordDao;
import cn.bestick.weightrecord.dao.StudyRecordDao;
import cn.bestick.weightrecord.dao.UserDao;
import cn.bestick.weightrecord.dto.DataWrapper;
import cn.bestick.weightrecord.dto.WrapperUser;
import cn.bestick.weightrecord.model.User;
import cn.bestick.weightrecord.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 * User: Bestick
 * Date: 2016/7/8
 * Time: 18:49
 * Url: http://www.bestick.cn
 * Copyright © 2015-2016 Bestick All rights reserved
 * -----------------------------------------------------------
 * 会当凌绝顶，一览众山小。
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RecordDao recordDao;

    @Autowired
    private StudyRecordDao studyRecordDao;

    /**
     * 用户登录
     *
     * @param user 入参
     * @return WrapperUser
     */
    public WrapperUser login(User user) {
        WrapperUser wrapperUser = new WrapperUser();
        try {
            User resultUser = userDao.login(user);
            if (resultUser != null) {
                wrapperUser.setData(resultUser);
                wrapperUser.setMsg("登录成功");
                return wrapperUser;
            }
            wrapperUser.setMsg("用户名或密码错误");
            wrapperUser.setStatus(-1);
            return wrapperUser;
        } catch (Exception e) {
            e.printStackTrace();
            wrapperUser.setMsg(e.getMessage());
            wrapperUser.setStatus(-2);
            return wrapperUser;
        }
    }

    /**
     * 用户注册
     *
     * @param user 入参
     * @return DataWrapper
     */
    public DataWrapper addUser(User user) {
        DataWrapper dataWrapper = new DataWrapper();
        try {
            if (StringUtil.isEmpty(user.getUserName()) || StringUtil.isEmpty(user.getPassword())) {
                dataWrapper.setStatus(-1);
                dataWrapper.setMsg("注册失败：用户名密码不能为空");
                return dataWrapper;
            }
            userDao.addUser(user);
            dataWrapper.setMsg("注册成功");
        } catch (Exception e) {
            dataWrapper.setStatus(-2);
            if (e.getMessage().contains(user.getUserName())) {
                dataWrapper.setMsg("注册失败：用户名 " + user.getUserName() + " 已被注册");
            } else if (e.getMessage().contains(user.getEmail())) {
                dataWrapper.setMsg("注册失败：邮箱 " + user.getEmail() + " 已被注册");
            } else {
                dataWrapper.setMsg("注册失败：" + e.getMessage());
                e.printStackTrace();
            }
        }
        return dataWrapper;
    }

    /**
     * 通过currentUser获取WrapperUser
     *
     * @param currentUser 当前登录用户
     * @return WrapperUser
     */
    public WrapperUser getCurrentUserInfo(User currentUser) {
        WrapperUser wrapperUser = new WrapperUser();
        try {
            if (currentUser == null) {
                wrapperUser.setStatus(-1);
                wrapperUser.setMsg("查询失败：您尚未登录");
                return wrapperUser;
            }
            Integer userId = currentUser.getUserId();
            User user = userDao.getUserById(userId);
            user.setRecordList(recordDao.getRecordsByUserId(userId));
            user.setStudyRecordList(studyRecordDao.getStudyRecordsByUserId(userId));
            wrapperUser.setData(user);
            wrapperUser.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            wrapperUser.setStatus(-2);
            wrapperUser.setMsg("查询失败：" + e.getMessage());
        }
        return wrapperUser;
    }
}